package com.sgb.link.api;


import android.util.Base64;

import com.sgb.link.BuildConfig;
import com.sgb.link.entity.LoginTempEntity;
import com.sgb.net.core.BaseCompatibleNet;
import com.sgb.net.core.UrlInterceptor;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.factory.PCCookieInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;

/**
 * @author: 杨畔
 * @date: 2021/2/19 16:43
 * @Description: 公用网络请求类
 */
public class CommonNet extends BaseCompatibleNet {

    private final CommonService mService;

    private CommonNet() {
        mService = getService(CommonService.class, BuildConfig.BASE_OLD_URL);
    }

    private static class Single {
        private static final CommonNet instance = new CommonNet();
    }

    public static CommonNet getInstance() {
        return Single.instance;
    }

    @Override
    protected void handleBuilder(OkHttpClient.Builder builder) {

    }

    @Override
    public void initNet() {
        //todo  更换接口前暂用老配置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //通用配置
        builder.addInterceptor(new UrlInterceptor(1))
                .addInterceptor(new PCCookieInterceptor())
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS);
        handleBuilder(builder);
        mClient = builder.build();
    }

    public CommonService getService() {
        return mService;
    }

    //-------------------------网络请求方法------------------------------

    /**
     * 单模块使用时登录接口，在模块入口activity或者application中调用，传入固定账号（暂存token用）
     */
    public Call<BaseEntity<LoginTempEntity>> login(String username, String password) {
        return mService.login(username, Base64.encodeToString(password.getBytes(), Base64.DEFAULT));
    }
}
